var i18n = {
    "loading_msg": {
        "en": "loading...",
        "zhcn": "接收电波中..."
    },
    "update_status": {
        "en": "Update Status",
        "zhcn": "更新进度"
    }
};
var _default_img = 'http://bgm.tv/img/info_only_m.png';
var login_error = false;
var offline = false;
var current_watching;
var current_calendar;
var current_eps;
var current_progress;
var current_subject;
var current_ep_id = 0;
var current_crt_id = 0;

var watching_has_created = false;
var calendar_has_created = false;
var subject_has_created = false;
var character_has_created = false;
var fav_has_created =false;
var _d = (new Date()).getDay() - 1;
var _today = (_d === -1) ? 6 : _d;

$(document).live("mobileinit", function ()
{
    //overwrite default settings
    $.mobile.loadingMessage = p('loading_msg');
});
$(document).ready(function ()
{


    if (window.location.hash && window.location.hash !== undefined)
    {
        if (window.location.hash === '#subject' || window.location.hash === '#watching' || window.location.hash === '#calendar' || window.location.hash === '#character' || window.location.hash === '#update')
        {
            window.location.href = './';
            return false;
        }
    }


    $(".crt_li").live('tap', crtLiOnTap);
    $(".status-btn").live('tap', statusBtnOnTap);
    $('#login_form').live('submit', loginOnSubmit);
	$('#collect_form').live('submit',collectOnSubmit);
    $(".calendar_li").live('tap', calLiOnTap);
    $(".subject_li").live('tap', subLiOnTap);
    $('#collect_cancel').live('tap',colCancelOnTap);

    $('#watching').live('pagebeforecreate', function ()
    {
        watching_has_created = true;
    });
	
    $('#fav').live('pagebeforeshow', function ()
    {
        $.mobile.hidePageLoadingMsg();

			prepareCollectionForm();

		//fav_has_created = true;
    });

    $('#calendar').live('pageshow', function ()
    {
        $.mobile.hidePageLoadingMsg();
    });

    $('#watching').live('pageshow', function ()
    {
        $.mobile.hidePageLoadingMsg();
    });

    $('#subject').live('pageshow', function ()
    {
		fav_has_created = false;
        $.mobile.hidePageLoadingMsg();
    });

    $('#calendar').live('pagebeforecreate', function ()
    {
        slog('calendar_pagebeforecreate');
        calendar_has_created = true;
    });

    $('#subject').live('pagebeforecreate', function ()
    {

        subject_has_created = true;

    });
	
	$('#character').live('pagebeforecreate', function ()
    {

        character_has_created = true;

    });
	
	

});

function goToMainPage()
{

    if (getCookie("th_username") && getCookie("th_auth"))
    {
        if (watching_has_created === false)
        {
            login();
        }
        else
        {
            $.mobile.changePage('#watching');
        }
    }
    else
    {
        goToLoginPage();
    }
    //check if login, go to either watching page or login page;
}

function d()
{}

function getEpInfoByID(ep_id)
{
    var r;
	if (current_eps)
		var x_len = current_eps.eps.length;
		for ( var x = 0; x < x_len ; x+=1){
                if (ep_id == current_eps.eps[x].id)
                { //lazy check, ingore the type here; cuz I'm lazy
					return current_eps.eps[x];
				
				}
		}
	return r;
	
	

}

function p(il8n_key)
{
    var txt = '';
    var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;
    userLang = userLang.toLowerCase();
    if (userLang.substring(0, 2) === 'zh')
    {
        txt = i18n[il8n_key].zhcn;
    }
    else
    {
        txt = i18n[il8n_key].en;
    }
    return txt;
}

function setLS(key, json)
{
    //Store json as string in localStorage
    if (typeof (localStorage) == 'undefined')
    {
        getCookie("!");
    }
    else
    {
        var str = JSON.stringify(json);
        if (str)
        {
            localStorage[key] = str;
        }
    }
}

function getLS(key)
{
    var json;
    if (typeof (localStorage) == 'undefined')
    {
        getCookie("!");
    }
    else
    {
        var data = localStorage[key];
        if (data)
        {
            json = JSON.parse(data);
        }
    }
    return json;
}

function goToLoginPage()
{
    $.mobile.changePage('#login');
}

function postRequest(target, paras, command, show_loading)
{
    if (!show_loading)
    {
        show_loading = true;
    }
    $.mobile.showPageLoadingMsg();
    $.post(target, paras, function (data)
    {
        if (data)
        {
            if (data.code != null)
            {
                if (data.code != 200 && data.code != '200')
                {
                    slog("Error code found.");
                    login_error = true;
                }
                else
                {
                    var json_data = data;
                    doSomethingWithData(json_data, command);
                }
            }
            else
            {
                var json_data = data;
                doSomethingWithData(json_data, command);
            }
        }
        else
        {
            //TODO: handle empty return
        }
    }, "json").success(function ()
    {}).error(function ()
    {
        log("呜咕，出错了，请重试 或 <a href='http://bgm.tv/signup'>注册</a>");
        login_error = true;
    }).complete(function ()
    {

        if (login_error === true)
        {
			
            log("呜咕，出错了，请重试 或 <a href='http://bgm.tv/signup'>注册</a>");
            login_error = false;
			$.mobile.hidePageLoadingMsg();
        }
        else
        {}
    });
}

function doSomethingWithData(data, command)
{
    switch (command)
    {
    case "createWatchingList":
        createWatchingList(data);
        break;
    case "createCalendarList":
        createCalendarList(data);
        break;
    case "createSubjectEps":
        createSubjectEps(data);
        break;
    case "updateEp":
        updateBtnCss(data);
        break;
	case "afterCollect":
		afterCollect(data);
		break;
    default:
        break;
    }
}

function login()
{
    if (watching_has_created === false)
    {
        postRequest("login.php", {
            username: $("#username").val(),
            password: $("#password").val()
        }, "createWatchingList");
    }
    else
    {
        $.mobile.changePage('#watching');
    }
}

function goToSubjectPage(id)
{
    postRequest("subject.php", {
        subject_id: id
    }, "createSubjectEps");
}

function goToCalendarPage()
{
    if (calendar_has_created === false)
    {
        var json = getLS('json.calendar');
        if (json)
        {
            $.mobile.showPageLoadingMsg();
            createCalendarList(json);
        }
        else
        {
            postRequest("calendar.php", {
                ilove: "bgm"
            }, "createCalendarList");
        }
    }
    else
    {
        $.mobile.showPageLoadingMsg();
        setTimeout("changePageTo('#calendar',true)", 500);
    }
}

function log(msg)
{
    $("#debug > span").html(msg);
    if (typeof (console) == "undefined")
    {

        }
    else
    {
        console.log(msg);
    }
}

function slog(msg)
{
    if (typeof (console) == "undefined")
    {
        getCookie("!");
    }
    else
    {
        console.log(msg);
    }
}

function getCookie(c_name)
{
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++)
    {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name)
        {
            return unescape(y);
        }
    }
}

function checkIfWatching(id)
{
    data_theme = "";
    if (current_watching && id)
    {
        for (i = 0; i < current_watching.length; i++)
        {
            if (id == current_watching[i].subject.id)
            {
                data_theme = 'data-theme = "b"';
                slog(data_theme);
                break;
            }
        }
    }
    return data_theme;
}

function createCalendarList(cal_json)
{
    slog(cal_json);
    current_calendar = cal_json;
    setLS('json.calendar', cal_json);
/*    $('#calendar-list').empty();
    $('#past-list').empty();
    $('#post-list').empty();*/


    var x_len = cal_json.length;
    for (var x = 0; x < x_len; x += 1)
    {
        var t = [];
        var i = 0;
        var y_len = cal_json[x].items.length;
        t[i++] = '<li data-role="list-divider">';
        t[i++] = cal_json[x].weekday.cn;
        t[i++] = '</li>';
        for (var y = 0; y < y_len; y += 1)
        {
            t[i++] = '<li' + ' class="calendar_li" id="';
            t[i++] = cal_json[x].items[y].id;
            t[i++] = '" ';
            t[i++] = checkIfWatching(cal_json[x].items[y].id);
            t[i++] = '>';
            t[i++] = '<a data-ajax="false" href="javascript:d();">';
            t[i++] = '<span class="thumbnail"><span class="image"><img src="';
            t[i++] = cal_json[x].items[y].images.medium;
            t[i++] = '"></span><span class="overlay"></span></span>';
            t[i++] = '<h1>';
            t[i++] = cal_json[x].items[y].name_cn;
            t[i++] = '</h1>';

        }

        if (_today === x)
        {
            $('#calendar-list').html(t.join(''));
        }
        else if (x < _today)
        {
            $('#pre-list').html(t.join(''));
        }
        else
        {
            $('#post-list').html(t.join(''));
        }


    }






    setTimeout(changePageTo('#calendar', true), 500);
}

function createWatchingList(watching_json)
{
    slog(watching_json);
    current_watching = watching_json;
    //setLS('json.watching', watching_json);
 
    var t = [];
    var i = 0;
    var x_len = watching_json.length;
	
    for (var x = 0; x < x_len; x += 1)
    {
        t[i++] = '<li' + ' class="subject_li" id="';
        t[i++] = watching_json[x].subject.id;
        t[i++] = '" ';
/*		t[i++] = (watching_json[x].subject.air_weekday == _today)?'data-theme="b"':'';  TODO: collection API doesn't provide a way to detect on air*/ 
		t[i++] = '>';
        t[i++] = '<a data-ajax="false" href="javascript:d();">';
        t[i++] = '<span class="thumbnail"><span class="image"><img src="';
        t[i++] = watching_json[x].subject.images.medium;
        t[i++] = '"></span><span class="overlay"></span></span>';
        t[i++] = '<h1>';
        t[i++] = watching_json[x].subject.name_cn;
        t[i++] = '</h1>';
        t[i++] = '<p><span class="progress" style="width:';
        t[i++] = parseInt(watching_json[x].ep_status) * 100 / parseInt(watching_json[x].subject.eps);
        t[i++] = '%">';
        t[i++] = watching_json[x].ep_status;
        t[i++] = '/';
        t[i++] = watching_json[x].subject.eps;
        t[i++] = '</span></p>';
        t[i++] = '<p class="ui-li-aside"></p>';
        t[i++] = '</a>';
        t[i++] = '</li>';
    }
    $('#watching-list').html(t.join(''));

    //also a little touch to the about page;
    if (getCookie('th_avatar') && getCookie('th_username'))
    {
        try
        {
            var t = [];
            var i = 0;
            t[i++] = '	<li data-role="list-divider">特别感谢</li><li><a href="http://bgm.tv/user/';
            t[i++] = getCookie('th_username');
            t[i++] = '"><img src="';
            t[i++] = getCookie('th_avatar');
            t[i++] = '" /><h4>+You</h4><span>一个好人</span></a></li>';
            $('#staff-list').append(t.join(''));
        }
        catch (err)
        {}
    }

    $.mobile.changePage("#watching", {
        transition: "slide"
    });
}

function createSubjectEps(eps_json)
{
    //setLS('json.current_ep', eps_json);
/*    $('#progress-list').empty();
    $('#subject > div > h1').empty();
*/
    var ep = eps_json.ep;
    var progress = eps_json.progress;
    var subject = eps_json.subject;
	var collection = eps_json.collection;
	current_collection = collection;
    current_eps = ep;
    current_progress = progress;
    current_subject = subject;
    slog(ep);
    slog(progress);
    slog(subject);
	slog(collection)
    var buttons = ""; //
    var t = [];
    var i = 0;
    var x_len = ep.eps.length;
    var y_len = (progress) ? progress.eps.length : 0;
    var did = false;
    var sort_num;
    var sort_len = ep.eps[ep.eps.length - 1].sort.toString().length;
    var status_id = 'NA';

    for (var x = 0; x < x_len; x += 1)
    {
        if (progress !== undefined)
        {
            for (y = 0; y < y_len; y += 1)
            {
                if (ep.eps[x].id === progress.eps[y].id)
                {
                    did = true;
                    status_id = progress.eps[y].status.css_name;
                }
            }
        }

        t[i++] = '<a id="ep';
        t[i++] = ep.eps[x].id;
        t[i++] = '" ep_id="';
        t[i++] = ep.eps[x].id;
        t[i++] = '" base-status="';
		t[i++] = ep.eps[x].status;
		t[i++] = '" class="';
        if (did !== true)
        {
            t[i++] = ep.eps[x].status;

        }
        else
        {
            t[i++] = status_id;
        }
        t[i++] = ' status-btn" href="#update" data-rel="dialog" data-transition="pop" data-inline="true" data-role="button" data-theme="a">';
        t[i++] = zeroFill(ep.eps[x].sort, sort_len);
        t[i++] = '</a>';

        did = false;
    }

    $('#progress-list').html(t.join(''));


    $('#subject > div > h1').html(ep.name_cn);
    //Finished loading status buttons; Now creating subject info
/*    $('#subject-info').empty();*/



    var crt = subject.crt;
    var staff = subject.staff;


    var t = [];
    var i = 0;

    t[i++] = '<div><dl><dt><h1>';
    t[i++] = subject.name;
	t[i++] = '<a href="#fav" data-rel="dialog" data-transition="pop" data-role="button" data-icon="star" data-iconpos="notext" id="fav_button">收藏</a></h1>'
	t[i++] = '</dt><dd>';
    t[i++] = subject.summary;
    t[i++] = '</dd><br></div><div><ul data-role="listview" data-theme="g" id="info-list">';

    if (crt)
    {
        var x_len = crt.length;
        t[i++] = '<li data-role="list-divider">登场角色</li>';
        for (var x = 0; x < x_len; x += 1)
        {

            var crt_img = (crt[x].images) ? crt[x].images.grid : _default_img;
            t[i++] = '<li' + ' class="crt_li" crt_id="';
            t[i++] = crt[x].id;
            t[i++] = '" ' + '>' + '<a data-ajax="false" href="javascript:d();"><span class="thumbnail"><span class="image"><img src="';
            t[i++] = crt_img;
            t[i++] = '"></span><span class="overlay"></span></span><h3>';
            t[i++] = crt[x].name;
            t[i++] = '</h3><p>';
            t[i++] = crt[x].role_name;
            t[i++] = '</p></a></li>';

        }
    }
    if (staff)
    {

        t[i++] = '<li data-role="list-divider">制作人员</li>';
        var s_len = staff.length;
        for (var s = 0; s < s_len; s += 1)
        {
            var staff_img = (staff[s].images) ? staff[s].images.grid : _default_img;

            t[i++] = '<li' + ' class="staff_li" id="staff';
            t[i++] = staff[s].id;
            t[i++] = '" ' + '><a data-ajax="false" href="javascript:goToBgmPrsnPage(';
			t[i++] = staff[s].id;
			t[i++] = ');"><span class="thumbnail"><span class="image"><img src="';
            t[i++] = staff_img;
            t[i++] = '"></span><span class="overlay"></span></span><h3>';
            t[i++] = staff[s].name;
			t[i++] = '</h3><span class="ui-li-count ui-btn-up-c ui-btn-corner-all">';
			t[i++] = (staff[s].comment)?staff[s].comment:0;
			t[i++] = '</span><p>';
			t[i++] = (staff[s].jobs)?(staff[s].jobs).join('，'):'';
            t[i++] = '</p></a></li>';


        }

    }

    t[i++] = '</ul></div>';
    $('#subject-info').html(t.join(''));

    if (subject_has_created === true)
    {
        $('#subject').page('destroy').page();

    }

    setTimeout("changePageTo('#subject',true)", 500);

}

function changePageTo(page_id, reset_page)
{
    if (reset_page === true)
    {

        }
    $.mobile.changePage(page_id, {
        transition: "slide"
    });
}

function zeroFill(str, str_len)
{
    str = str.toString();
    if (str.length < str_len && str)
    {
        for (i = 0; i < str_len - str.length; i++)
        {
            str = '0' + str;
        }
    }
    return str;
}

function removed()
{
    $("#ep" + current_ep_id).removeClass('NA Queue Watched Air Unknown');
    $("#ep" + current_ep_id).addClass('Drop');
    current_ep_id = 0;
    $('#update').dialog('close');
}

function watched()
{
    $("#ep" + current_ep_id).removeClass('Queue Drop Air NA Unknown');
    $("#ep" + current_ep_id).addClass('Watched');
    current_ep_id = 0;
    $('#update').dialog('close');
}

function canceled()
{
    $("#ep" + current_ep_id).removeClass('Watched Queue Drop Unknown');
	var base_status = $("#ep" + current_ep_id).attr('base-status')
    $("#ep" + current_ep_id).addClass(base_status);
    current_ep_id = 0;
    $('#update').dialog('close');
}

function queued()
{
    $("#ep" + current_ep_id).removeClass('Watched Drop Air NA Unknown');
    $("#ep" + current_ep_id).addClass('Queue');
    current_ep_id = 0;
    $('#update').dialog('close');
}

function doUpdate(stat)
{
	if (current_collection.rating){
    postRequest("update.php", {
        ep_id: current_ep_id,
        status: stat
    }, "updateEp");
	}
	else if (current_subject.id){
    postRequest("update.php", {
        ep_id: current_ep_id,
        status: stat,
		subject_id: current_subject.id
    }, "updateEp");
	}
}

function doCollect(){
	postRequest("collection.php", {
        collect_status: $('input[name=collect_status]:checked').val(),
        rating: $('#rating').val()+'',
		subject_id: current_subject.id
    }, "afterCollect");

}

function updateBtnCss(update_json)
{
    var update_url = update_json['request']; //suddenly not working as update_json.request, not sure why....
    var array = update_url.split('/');
    var q = array[4].split('?');
    var stat = q[0]
    if (stat === 'watched')
    {
        watched();
    }
    else if (stat === 'remove')
    {
        canceled();
    }
    else if (stat === 'drop')
    {
        removed();
    }
	else if (stat === 'queue'){
		queued();
	}
}

function getBase64Image(img_url)
{
    var img = new Image();
    img.src = img_url;
    img.onload = function ()
    {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL("image/png");
        base64 = dataURL;
        if (base64.length > 20)
        {
            setLS(img_url, base64);
        }
    }
}

function createCrtPage()
{
/*	$('#crt_main').empty();*/
    if (current_crt_id > 0)
    {
        var local_crt = getCrtByID(current_crt_id);
        if (local_crt)
        {
			var t = [];
			var i = 0;
			

            var crt_img = (local_crt.images)?local_crt.images.medium : _default_img;



			
			t[i++] = '<li id="crt_main"><span id="crt_clip"><img src="';
			t[i++] = crt_img;
			t[i++] = '" /></span><h1 id="crt_basic" style="font-size:';
			t[i++] = (local_crt.name.length > 4)? 1.3 : 2;
			t[i++] ='em">'
			t[i++] = local_crt.name;
			t[i++] = '</h1>';
			t[i++] = '</li>'; 

			
           // $('#crt_main').html(t.join(''));
			
            var crt_actor = local_crt.actors;
			if (crt_actor)
			{
			//var t = [];
			//var i = 0;
			
			var x_len = local_crt.actors.length;
			t[i++] = '<li data-role="list-divider">声优</li>'

			for (var x = 0; x<x_len; x+=1){
				var actor_img = (crt_actor[x].images) ? crt_actor[x].images.grid : _default_img;
				t[i++] = '<li' + ' class="actor_li" actor_id="';
				t[i++] = crt_actor[x].id;
				t[i++] = '" ' + '><span class="thumbnail"><span class="image"><img src="';
				t[i++] = actor_img;
				t[i++] = '"></span><span class="overlay"></span></span><h1>';
				t[i++] = crt_actor[x].name;
				t[i++] = '</h1></li>';
			}

			t[i++] = '<li data-role="list-divider">资料</li>';
			var power = parseInt(local_crt.comment*2+local_crt.collects/4);
			t[i++] = '<li><h3>战斗力</h3><p>';
			t[i++] = '<span class="progress" style="width:';
        	t[i++] = 5+power;
        	t[i++] = '%">';
			t[i++] = 5+power*power*power;
			t[i++] = '<span>';
			t[i++] = '</p></li>';
			t[i++] = (local_crt.name_cn !== '')?'<li><h3>中文名</h3><p>'+local_crt.name_cn+'</p></li>':'';
			if (local_crt.info){
			t[i++] = (local_crt.alias)?'<li><h3>别名</h3><p>'+local_crt.alias.join('，')+'</p></li>':'';
			t[i++] = (local_crt.info.birth !== '')?'<li><h3>生日</h3><p>'+local_crt.info.birth+'</p></li>':'';
			t[i++] = (local_crt.info.bloodtype !== '')?'<li><h3>血型</h3><p>'+local_crt.info.bloodtype+'</p></li>':'';
			t[i++] = (local_crt.info.bwh !== '')?'<li><h3>三围</h3><p>'+local_crt.info.bwh+'</p></li>':'';
			t[i++] = (local_crt.info.gender !== '')?'<li><h3>性别</h3><p>'+local_crt.info.gender+'</p></li>':'';
			t[i++] = (local_crt.info.height !== '')?'<li><h3>身高</h3><p>'+local_crt.info.height+'</p></li>':'';
			t[i++] = (local_crt.info.weight !== '')?'<li><h3>体重</h3><p>'+local_crt.info.weight+'</p></li>':'';
			}
			
			t[i++] = '<li data-theme="b"><a href="javascript:goToBgmCrtPage('+ local_crt.id +');"><h3>参与讨论</h3><span class="ui-li-count ui-btn-up-c ui-btn-corner-all">'+local_crt.comment+'</span></a></li>'
			$('#crt-info-list').html(t.join(''));
			}
			if (character_has_created === true){
				$('#crt-info-list').listview('refresh');
			}
			


            $.mobile.changePage("#character", {
                transition: "slide"
            });
        }
    }

}

function getCrtByID(local_crt_id)
{
    var target_crt = null;
    if (current_subject)
    {
        for (cr = 0; cr < current_subject.crt.length; cr++)
        {
            if (local_crt_id == current_subject.crt[cr].id)
            {
                target_crt = current_subject.crt[cr];
                break;
            }
        }
    }
    return target_crt;
}

function getImageFromLS(img_url)
{
    var return_val = img_url;
    var img = getLS(img_url);
    if (img)
    {
        return_val = img;
    }
    else
    {
        getBase64Image(img_url);
    }
    return return_val;
}

function goToBgmEpPage()
{
    if (current_ep_id !== 0)
    {
        window.open("http://bgm.tv/m/topic/ep/" + current_ep_id);
    }
}





function goToBgmCrtPage(id)
{
    if (id)
    {
        //http://bgm.tv/m/topic/crt/12347
        window.open("http://bgm.tv/m/topic/crt/" + id)
    }

}

function goToBgmPrsnPage(id)
{
    if (id)
    {
        //http://bgm.tv/m/topic/crt/12347
        window.open("http://bgm.tv/m/topic/prsn/" + id)
    }

}


function prepareCollectionForm(){
		if (fav_has_created === false){
		$("input[type='radio']").attr("checked",false);
		var rating = $("select#rating");
		rating[0].selectedIndex = 0;
		if (current_collection){
			if (current_collection.status){
				$('#'+current_collection.status.type).attr('checked',true);
			}
			if (current_collection.rating){
				rating[0].selectedIndex =  current_collection.rating;
			}
		}
	
			$("input[type='radio']").checkboxradio("refresh");
			rating.selectmenu("refresh");
			fav_has_created === true;
		}


}




var crtLiOnTap = function ()
    {
        slog('tap');
        current_crt_id = 0;
        current_crt_id = $(this).attr('crt_id');
        if (current_crt_id != 0)
        {
            createCrtPage();
        }
    };

var collectOnSubmit = function ()
{
	doCollect();
	return false;
}

var loginOnSubmit = function ()
    {
        login();
        return false;
    };

var statusBtnOnTap = function ()
    {
        current_ep_id = $(this).attr('ep_id');
		var ep = getEpInfoByID(current_ep_id);
		var name =  ep ?  ep.name : p('update_status');
		var comment =  ep ?  ep.comment : 0;
		var air_date = ep ?  ep.airdate : '';
		$('#update_status_header').html('ep.' + $(this).children().children().html() + " " + name);
		$('#air_date').html(air_date);
		$('.ep-comments-count').html(comment);

    }

var calLiOnTap = function ()
    {
        goToSubjectPage($(this).attr('id'));
    }

var subLiOnTap = function ()
    {
        goToSubjectPage($(this).attr('id'));
    }
	
var colCancelOnTap = function ()
{
$('#fav').dialog('close');
}

function afterCollect(data){
	current_collection = data;
	$.mobile.hidePageLoadingMsg();
	$('#fav').dialog('close');
	fav_has_created = false;
	slog('collect->');
	slog(data);
}